package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.system.runtai.entity.*;
import com.system.runtai.entity.VO.CompanyAnnouncementsDto;
import com.system.runtai.service.*;
import com.system.common.result.Result;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公司介绍 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "公司介绍")
@RestController
@RequestMapping("/runtai/company-introduction")
public class CompanyIntroductionController {

    @Autowired
    private CompanyIntroductionService companyIntroductionService;

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @Autowired
    private ArticleCollectService articleCollectService;


    @Autowired
    private CompanyAnnouncementsService companyAnnouncementsService;

    @Autowired
    private InRegardOfService inRegardOfService;

    @Autowired
    private CompanyDynamicService companyDynamicService;

    @Autowired
    private CompanyCaseService companyCaseService;


    @ApiOperation(value = "公司介绍二级")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<List<SecondaryCopywriter>> getSecondaryCopywriter(@PathVariable Integer id,@RequestParam(required = false) String title){
        if (StringUtils.isEmpty(title)) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 2).eq(SecondaryCopywriter::getMainArgumentId, id);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }else if (!StringUtils.isEmpty(title)){
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 2).eq(SecondaryCopywriter::getMainArgumentId, id).like(SecondaryCopywriter::getTitle,title);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }

        List<SecondaryCopywriter> list = null;
        return Result.fail(list).message("对象为空");
    }


    @ApiOperation(value = "添加公司介绍二级文案")
    @PostMapping("/saveSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setCreateTime(LocalDateTime.now());
        secondaryCopywriter.setStartid(2);
        secondaryCopywriter.setMainArgumentId(id);
        boolean save = secondaryCopywriterService.save(secondaryCopywriter);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }


    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer id){

        boolean b = secondaryCopywriterService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "所有公司介绍")
    @GetMapping("/companyIntroductionList")
    public Result<List<CompanyIntroduction>> companyIntroductionList(){
        List<CompanyIntroduction> list = companyIntroductionService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "后台公司介绍详情")
    @GetMapping("/companyIntroductionht/{id}")
    public Result<CompanyIntroduction> companyIntroductionByidht(@PathVariable Integer id) {

        CompanyIntroduction byId = companyIntroductionService.getById(id);
            return Result.ok(byId);

    }

    @ApiOperation(value = "程序公司介绍详情")
    @GetMapping("/companyIntroductioncx/{id}")
    public Result<CompanyIntroduction> companyIntroductionByidcx(@PathVariable Integer id) {

        String s = AuthContextUtil.get();
        CompanyIntroduction byId = companyIntroductionService.getById(id);
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getStartId, 2).eq(ArticleCollect::getInRegardOfId, id).eq(ArticleCollect::getAppletUserId,Integer.valueOf(s));
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one == null){
            byId.setCollect(0);
            return Result.ok(byId);
        }else {
            byId.setCollect(1);
            return Result.ok(byId);
        }

    }

    /**
     * TODO 类型的判断进行查询相应数据
     * @param startId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查看所有公司介绍一、二级文案")
    @GetMapping("/getCompanyIntroductionSC/{startId}")
    public Result<IPage<CompanyIntroduction>> getCompanyIntroductionList(@PathVariable Integer startId,@RequestParam(required = false) Long page, @RequestParam(required = false) Long limit){
        List<CompanyIntroduction> list = companyIntroductionService.list();

        Page<CompanyIntroduction> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CompanyIntroduction> wrapper = new LambdaQueryWrapper<>();
        Integer id = null;
        for (CompanyIntroduction companyIntroduction : list) {
            id = companyIntroduction.getId();
        }

        if (id!=null){
            wrapper.eq(CompanyIntroduction::getId,id).orderByDesc(CompanyIntroduction::getCreateTime);
        }

        IPage<CompanyIntroduction> iPage = companyIntroductionService.page(recommendPage,wrapper);
        for (CompanyIntroduction record : iPage.getRecords()) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,record.getId());
            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper1);
            record.setSecondaryCopywriterList(copywriterList);

        }
        return Result.ok(iPage);
    }




    @ApiOperation(value = "添加公司简介")
    @PostMapping("/saveCompanyIntroduction")
    public Result<CompanyIntroduction> saveCompanyIntroduction(@RequestBody CompanyIntroduction companyIntroduction){

        companyIntroduction.setCreateTime(LocalDateTime.now());
        boolean save = companyIntroductionService.save(companyIntroduction);

        if (save){
            return Result.ok(companyIntroduction).message("添加成功");
        }else {
            return Result.ok(companyIntroduction).message("添加失败");
        }
    }

    @ApiOperation(value = "删除关于润泰文章")
    @PostMapping("delectCompanyIntroduction/{start}/{id}")
    public Result delectCompanyIntroduction(@PathVariable Integer start,@PathVariable Integer id){


        if (start==1){

            boolean save = inRegardOfService.removeById(id);
            LambdaQueryWrapper<SecondaryCopywriter> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getMainArgumentId,id).eq(SecondaryCopywriter::getStartid,start);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list();
            for (SecondaryCopywriter secondaryCopywriter : list) {
                boolean byId = secondaryCopywriterService.removeById(secondaryCopywriter);
                if (byId){
                    return Result.ok().message("删除成功");
                }
                return Result.fail().message("删除失败");
            }

            if (save){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");
        }else if (start==2){

            boolean save = companyIntroductionService.removeById(id);
            LambdaQueryWrapper<SecondaryCopywriter> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getMainArgumentId,id).eq(SecondaryCopywriter::getStartid,start);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list();
            for (SecondaryCopywriter secondaryCopywriter : list) {
                boolean byId = secondaryCopywriterService.removeById(secondaryCopywriter);
                if (byId){
                    return Result.ok().message("删除成功");
                }
                return Result.fail().message("删除失败");
            }

            if (save){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");
        }else if (start==3){

            boolean save = companyDynamicService.removeById(id);
            LambdaQueryWrapper<SecondaryCopywriter> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getMainArgumentId,id).eq(SecondaryCopywriter::getStartid,start);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list();
            for (SecondaryCopywriter secondaryCopywriter : list) {
                boolean byId = secondaryCopywriterService.removeById(secondaryCopywriter);
                if (byId){
                    return Result.ok().message("删除成功");
                }
                return Result.fail().message("删除失败");
            }

            if (save){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");

        }else if (start==4){

            boolean save = companyCaseService.removeById(id);
            LambdaQueryWrapper<SecondaryCopywriter> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getMainArgumentId,id).eq(SecondaryCopywriter::getStartid,start);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list();
            for (SecondaryCopywriter secondaryCopywriter : list) {
                boolean byId = secondaryCopywriterService.removeById(secondaryCopywriter);
                if (byId){
                    return Result.ok().message("删除成功");
                }
                return Result.fail().message("删除失败");
            }

            if (save){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");

        }else if (start==5){

            boolean save = companyAnnouncementsService.removeById(id);
            LambdaQueryWrapper<SecondaryCopywriter> wrapper =new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getMainArgumentId,id).eq(SecondaryCopywriter::getStartid,start);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list();
            for (SecondaryCopywriter secondaryCopywriter : list) {
                boolean byId = secondaryCopywriterService.removeById(secondaryCopywriter);
                if (byId){
                    return Result.ok().message("删除成功");
                }
                return Result.fail().message("删除失败");
            }
            if (save){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");

        }

        return Result.fail().message("没有该信息！");

    }


    @ApiOperation(value = "修改公司简介")
    @PostMapping("/updateCompanyIntroduction/{start}")
    public Result updateCompanyIntroduction(@PathVariable Integer start,@RequestBody CompanyAnnouncementsDto companyAnnouncements){

        if (companyAnnouncements.getStart()==1){
            InRegardOf byId = inRegardOfService.getById(companyAnnouncements.getId());

            byId.setUpdateTime(LocalDateTime.now());
            byId.setTitle(companyAnnouncements.getTitle());
            byId.setArticle(companyAnnouncements.getArticle());
            byId.setPicture(companyAnnouncements.getPicture());
            boolean save = inRegardOfService.updateById(byId);

            if (save){
                return Result.ok().message("修改成功");
            }
            return Result.fail().message("修改失败");
        }else if (companyAnnouncements.getStart()==2){

            CompanyIntroduction companyIntroduction = companyIntroductionService.getById(companyAnnouncements.getId());
            companyIntroduction.setTitle(companyAnnouncements.getTitle());
            companyIntroduction.setArticle(companyAnnouncements.getArticle());
            companyIntroduction.setPicture(companyAnnouncements.getPicture());
//            companyIntroduction.setVideo(companyAnnouncements.getVideo());
//            companyIntroduction.setStart(2);
            companyIntroduction.setUpdateTime(LocalDateTime.now());
            boolean save = companyIntroductionService.updateById(companyIntroduction);

            if (save){
                return Result.ok(companyIntroduction).message("修改成功");
            }else {
                return Result.fail(companyIntroduction).message("修改失败");
            }
        }else if (companyAnnouncements.getStart()==3){

            CompanyDynamic companyDynamic = companyDynamicService.getById(companyAnnouncements.getId());
            companyDynamic.setTitle(companyAnnouncements.getTitle());
            companyDynamic.setArticle(companyAnnouncements.getArticle());
            companyDynamic.setPicture(companyAnnouncements.getPicture());
//            companyDynamic.setVideo(companyAnnouncements.getVideo());
//            companyDynamic.setStart(3);
            companyDynamic.setUpdateTime(LocalDateTime.now());
            boolean save = companyDynamicService.updateById(companyDynamic);

            if (save){
                return Result.ok(companyDynamic).message("修改成功");
            }else {
                return Result.fail(companyDynamic).message("修改失败");
            }

        }else if (companyAnnouncements.getStart()==4){

            CompanyCase companyCase = companyCaseService.getById(companyAnnouncements.getId());
            companyCase.setTitle(companyAnnouncements.getTitle());
            companyCase.setArticle(companyAnnouncements.getArticle());
            companyCase.setPicture(companyAnnouncements.getPicture());
//            companyCase.setVideo(companyAnnouncements.getVideo());
//            companyCase.setStart(4);
            companyCase.setUpdateTime(LocalDateTime.now());
            boolean save = companyCaseService.updateById(companyCase);

            if (save) {
                return Result.ok(companyCase).message("修改成功");
            } else {
                return Result.fail(companyCase).message("修改失败");
            }

        }else if (companyAnnouncements.getStart()==5){
            CompanyAnnouncements announcements = companyAnnouncementsService.getById(companyAnnouncements.getId());
            announcements.setTitle(companyAnnouncements.getTitle());
            announcements.setArticle(companyAnnouncements.getArticle());
            announcements.setPicture(companyAnnouncements.getPicture());
//            announcements.setVideo(companyAnnouncements.getVideo());
//            announcements.setStart(5);
            announcements.setUpdateTime(LocalDateTime.now());
            boolean save = companyAnnouncementsService.updateById(announcements);
            if (save){
                return Result.ok(companyAnnouncements).message("修改成功");
            }else {
                return Result.fail(companyAnnouncements).message("修改失败");
            }

        }

        return Result.fail().message("没有该类型参数！");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CompanyIntroduction>> indexdas(@PathVariable Long page, @PathVariable Long limit, CompanyIntroduction companyIntroduction){

        Page<CompanyIntroduction> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CompanyIntroduction> wrapper = new LambdaQueryWrapper<>();

        Integer id = companyIntroduction.getId();

        if (id!=null){
            wrapper.eq(CompanyIntroduction::getId,id);
        }

        IPage<CompanyIntroduction> iPage = companyIntroductionService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }


    @ApiOperation(value = "收藏公司介绍：2")
    @PostMapping("collectCompanyCase")
    public Result collectCompanyCase(@RequestBody CompanyIntroduction companyIntroduction) {

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyIntroduction.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,2);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setCreateTime(LocalDateTime.now());
            articleCollect.setAppletUserId(Integer.valueOf(s));
            articleCollect.setInRegardOfId(companyIntroduction.getId());
            articleCollect.setStartId(2);
            boolean b = articleCollectService.save(articleCollect);

            if (b) {
                return Result.ok().message("添加收藏成功");
            }
            return Result.fail().message("添加收藏失败");
        }
    }


    @ApiOperation(value = "删除公司介绍收藏")
    @PostMapping("delectCompanyIntroductionCollect")
    public Result delectCompanyIntroductionCollect(@RequestBody CompanyIntroduction companyIntroduction) {
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyIntroduction.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,2);
        boolean b = articleCollectService.removeById(wrapper);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }




    @ApiOperation(value = "查询公告")
    @GetMapping("/selectAffiche")
    public Result selectAffiche(@RequestParam(required = false) Integer startId,@RequestParam(required = false) Long page, @RequestParam(required = false) Long limit) {

        if (startId == 1) {

            Page<InRegardOf> recommendPage = new Page<>(page,limit);
            IPage<InRegardOf> iPage = inRegardOfService.page(recommendPage);
            return Result.ok(iPage);
        } else if (startId == 2) {

            Page<CompanyIntroduction> recommendPage = new Page<>(page,limit);
            IPage<CompanyIntroduction> iPage = companyIntroductionService.page(recommendPage);
            return Result.ok(iPage);

        } else if (startId == 3) {

            Page<CompanyDynamic> recommendPage = new Page<>(page,limit);
            IPage<CompanyDynamic> iPage = companyDynamicService.page(recommendPage);
            return Result.ok(iPage);

        } else if (startId == 4) {
            Page<CompanyCase> recommendPage = new Page<>(page,limit);
            IPage<CompanyCase> iPage = companyCaseService.page(recommendPage);
            return Result.ok(iPage);

        } else if (startId == 5) {
            Page<CompanyAnnouncements> recommendPage = new Page<>(page,limit);
            IPage<CompanyAnnouncements> iPage = companyAnnouncementsService.page(recommendPage);
            return Result.ok(iPage);

        }
        return Result.fail().message("没有该类型参数！");
    }
}

